//
//  AppDelegate.swift
//  weibo-snh
//
//  Created by 黄力 on 15/12/14.
//  Copyright © 2015年 HLTest. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //注册成为监听者
        registerAddObserver()
        //创建窗口
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //设置窗口的根控制器
        window?.rootViewController = showController()
        //设置当前窗口为可视主窗口
        window?.makeKeyAndVisible()
        return true
    }
    
    //注册成为监听者
    private func registerAddObserver() {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootController:", name: MainWindowRootControllerChangeNotification, object: nil)
    }
    
    //接收到更改根控制器的通知后调用该方法
    @objc private func changeRootController(notification: NSNotification) {
        //根据object参数是否有值来设置不同的根控制器
        if notification.object == nil {
            window?.rootViewController = HLWelcomeController()
            return
        }
        window?.rootViewController = HLMainController()
    }
    
    //获取当前应该显示的控制器
    private func showController() -> UIViewController {
        //获取当前的登录状态
        if HLUserAccountViewModel().loginStatus {
            //判断当前是否是新版本
            if compareVisionNum() {
                return HLGudieController()
            }else {
                return HLWelcomeController()
            }
        }
        return HLMainController()
    }
    
    //比较当前版本和保存的版本,判断是否是新版本
    private func compareVisionNum() -> Bool {
        //获取当前版本
        let newVisionNum = (NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! NSString).doubleValue
        //获取沙盒中保存的版本号
        let saveVisionNum = NSUserDefaults.standardUserDefaults().doubleForKey("visionNum")
        //比较版本号
        if newVisionNum > saveVisionNum {
            //新版本
            //保存到沙盒
            NSUserDefaults.standardUserDefaults().setDouble(newVisionNum, forKey: "visionNum")
            return true
        }
        
        return false
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

